<template>
	<view>
	<view class="search-box">
		 <uni-search-bar @input="input" placeholder="请输入搜索的内容" :radius="50" :focus="true" cancelButton="none"></uni-search-bar>
		 </view>
		  <!-- 搭建建议列表 -->
		 <view class="sugg-list" v-if="searchResult.length !== 0">
		 	    <view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="goToList(item.goods_id)">
		 		<view class="goods-name">{{ item.goods_name}}</view>
		 		<uni-icons type="arrow-right" size="16"></uni-icons>
		</view>
		 </view>
		 <!-- 搜索历史 -->
		 <view class="search-history" v-else>
			 <!-- 搜素标题 -->
			 <view class="search-title">
				 <text>搜索历史</text>
				 <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
			 </view>
			 <!-- 搜索历史列表 -->
			 <view class="search-history-list">
				 <uni-tag :text="item" :circle="true" v-for="(item,i) in newHistoryList" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			 </view>
		 </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖
				timer:null,
				// 用户输入的内容
				kw:'',
				// 搜索结果列表
				searchResult:[],
				// 搜素历史列表
				historyList:['as','gfd', 'sgsh']
			};
		},
		computed:{
			newHistoryList(){
				// 数据是引用类型，不能直接用reverse,以免引起元素错乱的问题
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
		  this.historyList = JSON.parse(uni.getStorageSync('key') || '[]')
		},
		methods:{
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					// console.log(e)
					this.kw = e.trim()
					this.getSearchResult()
				},500)
			},
		async getSearchResult(){
				if(this.kw === ''){
					this.searchResult = []
					return
				}
			const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{ query:this.kw})
			if(res.meta.status !== 200) return uni.$showMge()
			this.searchResult = res.message
			this.saveHistoryList()
			},
			// 点击搜索建议跳转商品详情
			goToList(goods_id){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			// 用户输入的内容添加到搜索历史，并把历史本地保存
			saveHistoryList(){
				// 元素会重复
				// this.historyList.push(this.kw)
				// 利用Set对象去重
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				// 把set对象转化为数组
				this.historyList = Array.from(set)
				// 把浏览历史存储到本地
				uni.setStorageSync('key',JSON.stringify(this.historyList))
			},
			// 点击清除图标清除搜索历史，本地历史也清除
			clearHistory(){
				this.historyList = []
				uni.setStorageSync('key','[]')
			},
			// 点击搜索历史跳转到商品列表页面
			gotoGoodsList(kw){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		}
	}
</script>

<style lang="scss">
.search-box{
		position: sticky;
		top: 0;
		z-index: 999;
		 
}
.sugg-list{
	padding: 0 5px;
}
.sugg-item{
	padding: 13px 0;
	display: flex;
	align-items: center;
	justify-content: space-between;
	border-bottom: 1px solid #F3A73F;
}
.goods-name{
	font-size: 12px;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	margin-right: 5px;
}
.search-history{
	padding: 0 5px;
}
.search-title{
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 40px;
	font-size: 13px;
	border-bottom: 1px solid #efefef;
}
.search-history-list{
	display: flex;
	flex-wrap: wrap;
	margin-top: 10px;
	.uni-tag{
		margin-right: 15px;
		
	}
}
</style>
